<script lang="ts" setup>
import { reactive } from 'vue'
import { User, Lock, Message, Position, ChatRound} from '@element-plus/icons-vue'

const formInline = reactive({
    region1: '',
    region2: '',
})

const onSubmit = () => {
    console.log('submit!')
}
import { ref } from 'vue'

const currentPage = ref(1)
const pageSize = ref(4)
const name = ref('')
const totalpage = ref()
const small = ref(false)
const background = ref(false)
const disabled = ref(false)
const dialogVisible = ref(false)
const dialogname = ref()
const displaydel = ref(false)



const handleSizeChange = (size) => {
    pageSize.value = size
    sectionData()
}
const handleCurrentChange = (current) => {
    currentPage.value = current
    sectionData()
}
import {
    Delete,
    Edit,
    WarningFilled
} from '@element-plus/icons-vue'

import { sectionlistServer } from '@/api/section.js';
import { ElMessage, FormRules } from 'element-plus';

const tableData = ref()
const createtime = ref('')
const updatetime = ref('')

const reg = /^[\u4e00-\u9fa5]+$/

interface RuleForm {
    name: String,
    sId: Number,
    sd: String,
    
}


const addData = ref<RuleForm>({
    name: '',
    sId: null,
    sd: '',

})



const sectionData = async () => {
    let params = {
        countpage: currentPage.value,
        pagenumber: pageSize.value,
        name: name.value ? name.value : null,
        createtime: createtime.value ? moment(createtime.value).format(timeForm) : null,
        updatetime: updatetime.value ? moment(updatetime.value).format(timeForm) : null
    }

    let result = await sectionlistServer(params)

    tableData.value = result.data.items
    totalpage.value = result.data.total
}


sectionData();

const clearadddata = () => {
    addData.value = {
        name: '',
        sId: null,
        sd: '',
    }
}

import { addsectionServer, updatasectionServer, delsectionServer } from '@/api/section.js';
import moment from 'moment';
const addsection = async () => {
    dialogVisible.value = false;
    let result = await addsectionServer(addData.value) as any;
    ElMessage.success(result.message ? result.message : '添加成功')
    clearadddata()
    sectionData()
}

const updatasection = async () => {
    dialogVisible.value = false;
    let result = await updatasectionServer(addData.value) as any;
    ElMessage.success(result.message ? result.message : '修改成功')
    clearadddata()
    sectionData()
}

const delsection = async () => {
    displaydel.value = false
    let result = await delsectionServer(addData.value.sId) as any;
    ElMessage.success(result.message ? result.message : '删除成功')
    clearadddata()
    sectionData()
}

const rules = reactive<FormRules<RuleForm>>({
    name: [
        { required: true, message: '请输入版块名称', trigger: 'blur' },
        { pattern:reg, message: '要求全中文', trigger: 'blur' }
    ],

    sd: [
        { required: true, message: '请输入版块简介', trigger: 'blur' },
        { pattern: reg, message: '要求全中文', trigger: 'blur' }
    ]
})


const showupdataDiglog = (row) => {
    dialogVisible.value = true
    dialogname.value = '修改版块信息'
    addData.value.name = row.name
    addData.value.sd = row.sd
    addData.value.sId = row.sid

}

const showdelDiglog = (row) => {
    displaydel.value = true
    dialogname.value = '提示'
    addData.value.sId = row.sid

}

const timeForm = "YYYY-MM-DD HH:mm:ss"

const pFormat = (row) => {
    const time = row.createTime
    return moment(time).format('YYYY-MM-DD HH:mm')
}

const fFormat = (row) => {
    const time = row.updateTime
    return moment(time).format('YYYY-MM-DD HH:mm')
}

const shortcuts = [
    {
        text: '今天',
        value: new Date(),
    },
    {
        text: '昨天',
        value: () => {
            const date = new Date()
            date.setDate(date.getDate() - 1)
            return date
        },
    },
    {
        text: '明天',
        value: () => {
            const date = new Date()
            date.setDate(date.getDate() + 1)
            return date
        },
    },
    {
        text: '一周以后',
        value: () => {
            const date = new Date()
            date.setDate(date.getDate() + 7)
            return date
        },
    },
]
</script>


<template>

    <el-card style="max-width: 100%">
        <div class="card-header">
            <span>版块管理</span>
            <el-button type="primary" @click="dialogVisible = true, dialogname = '添加版块'">添加版块</el-button>
        </div>
        <div style="margin-top: 20px;">
            <hr>
        </div>
        <el-form :inline="true" class="form-inline"  >
            <el-form-item label="版块名称" prop="name">
                <el-input placeholder="请输入待查找版块名" v-model="name"></el-input>
            </el-form-item>
            <el-form-item label="开始时间" prop="starttime">
                <el-date-picker v-model="createtime" type="datetime" placeholder="选择创建日期和时间" :shortcuts="shortcuts" />
            </el-form-item>
            <el-form-item label="结束时间" prop="endtime">
                <el-date-picker v-model="updatetime" type="datetime" placeholder="选择更新日期和时间" :shortcuts="shortcuts" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="sectionData()">搜索</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="default" @click="name = '',createtime='',updatetime='', sectionData()">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="tableData" style="width: 100%" table-layout='auto'>
            <el-table-column prop="sid" label="id"></el-table-column>
            <el-table-column prop="name" label="版块名称" />
            <el-table-column prop="sd" label="版块简介" />
            <el-table-column prop="mainposts" label="主贴数" />
            <el-table-column prop="createTime" label="创建时间" :formatter="pFormat"/>
            <el-table-column prop="updateTime" label="更新时间" :formatter="fFormat"/>
            <el-table-column label="操作" width="180">
                <template #default="{ row }">
                    <el-button type="primary" :icon="Edit" circle @click="showupdataDiglog(row)" />
                    <el-button type="danger" :icon="Delete" circle @click="showdelDiglog(row)" />
                </template>
            </el-table-column>
        </el-table>

        <el-dialog v-model="dialogVisible" :title="dialogname" width="500">
            <el-form ref="form" class="add"  :model="addData">
                <el-form-item label="版块名称" prop="name">
                    <el-input  placeholder="请输入版块名称" v-model="addData.name"></el-input>
                </el-form-item>
                <el-form-item label="版块简介" prop="sd" >
                    <el-input   placeholder="请输入版块简介" v-model="addData.sd"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogVisible = false, clearadddata()">取消</el-button>
                    <el-button type="primary"
                        @click="dialogname == '添加版块' ? addsection() : updatasection()">确认</el-button>
                </div>
            </template>
        </el-dialog>

        <el-dialog v-model="displaydel" :title=dialogname width="500">
            <el-icon color="red">
                <WarningFilled />
            </el-icon>
            <span style="text-align: center;display:block;font-size: 17px;">是否确认删除该版块</span>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="displaydel = false, clearadddata()">否</el-button>
                    <el-button type="primary" @click="delsection()">是</el-button>
                </div>
            </template>
        </el-dialog>



        <!--layout控制组件展示顺序-->
        <el-pagination class="el-p" v-model:current-page="currentPage" v-model:page-size="pageSize"
            :page-sizes="[4, 8, 12, 16]" :small="small" :disabled="disabled" :background="background"
            layout="jumper, prev, pager, next, sizes, total" :total="totalpage" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
    </el-card>
</template>

<style scoped>
.el-p {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
}

.form-inline .el-input {
    --el-input-width: 220px;
}

.form-inline .el-select {
    --el-select-width: 220px;
}

.card-header {
    display: flex;
    justify-content: space-between;
    margin-inline-end: 20px;
}

.demo-datetime-picker {
    display: flex;
    width: 100%;
    padding: 0;
    flex-wrap: wrap;
}
</style>